import Vue from 'vue'
import VueRouter from 'vue-router'
import main from '../views/main.vue'
import store from '../store/index.js'
import _cache from '../utils/cache.js'

import { Message } from 'element-ui';

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject){
    return originalPush.call(this, location, onResolve, onReject)
  }
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

import NProgress from "nprogress"; // 进度条；进度指示器
import "nprogress/nprogress.css"; // 进度条；进度指示器 样式
// 配置
NProgress.inc(0.2);
NProgress.configure({
	easing: "ease",
	speed: 500,
	showSpinner: false
});

const routes = [
	{
		path: '/',
		name: 'main',
		component: main,
		redirect: '/forum-list',
		children: [
			{
				path: '/home',
				name: 'home',
				component: () => import('../views/home.vue'),
				meta: {
					needLogin: false
				},
			},
			{
				path: '/index',
				name: 'index',
				component: () => import('../views/index.vue'),
				meta: {
					needLogin: false
				},
			},
			{
				path: '/forum-list',
				name: 'forum-list',
				component: () => import( '../views/forum-list.vue'),
				meta: {
					needLogin: false
				},
			},
			{
				path: '/forum-detail/:id',
				name: 'forum-detail',
				component: () => import( '../views/forum-detail.vue'),
				meta: {
					root: '论坛',
					needLogin: false
				},
			},
			{
				path: '/know-list',
				name: 'know-list',
				component: () => import( '../views/know-list.vue'),
				meta: {
					needLogin: false
				},
			},
			{
				path: '/know-detail/:id',
				name: 'know-detail',
				component: () => import( '../views/know-detail.vue'),
				meta: {
					needLogin: false
				},
			},
			{
				path: '/notice-list',
				name: 'notice-list',
				component: () => import( '../views/notice-list.vue'),
				meta: {
					needLogin: false
				},
			},
			{
				path: '/notice-detail/:id',
				name: 'notice-detail',
				component: () => import( '../views/notice-detail.vue'),
				meta: {
					root: '公告',
					needLogin: false
				},
			},
			{
				path: '/study-list',
				name: 'study-list',
				component: () => import( '../views/study-list.vue'),
				meta: {
					needLogin: false
				},
			},
			{
				path: '/study-list2/:id',
				name: 'study-list2',
				component: () => import( '../views/study-list2.vue'),
				meta: {
					root: '知识库',
					needLogin: false
				},
			},
			{
				path: '/study-detail/:id',
				name: 'study-detail',
				component: () => import( '../views/study-detail.vue'),
				meta: {
					root: '知识库',
					needLogin: false
				},
			},
			{
				path: '/center/main',
				name: 'center-main',
				redirect: '/center/info',
				component: () => import( '../views/center/main.vue'),
				meta: {
					needLogin: true
				},
				children: [
					{
						path: '/center/info',
						name: 'center-info',
						component: () => import( '../views/center/info.vue'),
						meta: {
							needLogin: true
						},
					},
					{
						path: '/center/forum',
						name: 'center-forum',
						component: () => import( '../views/center/forum.vue'),
						meta: {
							needLogin: true
						},
					},
					{
						path: '/center/know',
						name: 'center-know',
						component: () => import( '../views/center/know.vue'),
						meta: {
							needLogin: true
						},
					},
					{
						path: '/center/study',
						name: 'center-study',
						component: () => import( '../views/center/study.vue'),
						meta: {
							needLogin: true
						},
					},
					{
						path: '/center/safe',
						name: 'center-safe',
						component: () => import( '../views/center/safe.vue'),
						meta: {
							needLogin: true
						},
					},
					{
						path: '/center/point',
						name: 'center-point',
						component: () => import( '../views/center/point.vue'),
						meta: {
							needLogin: true
						},
					}
				]
			}
		],
		meta: {
			needLogin: false
		},
	},
	{
		path: '/login',
		name: 'login',
		component: () => import(/* webpackChunkName: "about" */ '../views/login.vue'),
		meta: {
			needLogin: false
		}
	},
	{
		path: '/reset-pass',
		name: 'reset-pass',
		component: () => import(/* webpackChunkName: "about" */ '../views/reset-pass.vue'),
		meta: {
			needLogin: false
		},
	}
]

const scrollBehavior = (to, from, savedPosition) => {
	if (savedPosition) {
		return savedPosition
	} else {
		return {
			x: 0,
			y: 0
		}
	}
}

const router = new VueRouter({
	routes,
	scrollBehavior
})

router.beforeEach((to, from, next) => {
	let token = _cache.get('token')
	if (token) {
		store.dispatch('vuex_get_user_info')
		store.commit('setLoginStatus', true)
	}

	if (to.meta.needLogin && !store.state.isLogin) {
		Message({
			type: 'error',
			message: '请登录后再操作',
			duration: 1500
		})

		next(from.meta.needLogin ? '/' : from.fullPath);
	} else {
		NProgress.start();
		next();
	}

});
router.afterEach(() => {
	NProgress.done();
});

export default router
